package com.sita.linboard.DriverVehicle;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.sita.linboard.MainMessage.DriverDayMsgRequest;
import com.sita.linboard.R;
import com.sita.linboard.base.BaseActivity;
import com.sita.linboard.base.BaseApplication;
import com.sita.linboard.http.RestClient;
import com.sita.linboard.login.RegisterThirdActivity;
import com.sita.linboard.utils.SpUtils;
import com.sita.linboard.utils.ToastUtils;
import com.squareup.picasso.Picasso;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by Jason on 2016/9/20.
 */
public class DriverUnBindVehicleActivity extends BaseActivity implements View.OnClickListener,
        UnBindVehicleListener, SwipeRefreshLayout.OnRefreshListener {
    private LinearLayout back;
    private TextView head;
    private ImageView vehcile_img;
    private ListView bindVehicle_list;
    private Button addVehicle;
    private VehicleAdapter adapter;
    private SwipeRefreshLayout swipeRefreshLayout;
    private VehicleBackBean backBean;

    public static void DriverIntent(Context context) {
        Intent intent = new Intent(context, DriverUnBindVehicleActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initData() {
        head.setText("我的车辆");
        adapter = new VehicleAdapter(this, this);
        addVehicle.setOnClickListener(this);
        bindVehicle_list.setAdapter(adapter);
        swipeRefreshLayout.setOnRefreshListener(this);
    }
    @Override
    protected void onStart() {
        super.onStart();
        getDataFromSer();
    }

    private void getDataFromSer() {
        DriverDayMsgRequest request = new DriverDayMsgRequest();
        request.user_id = SpUtils.getString("AccoundID", null, BaseApplication.getContext());
        RestClient.getRestService().AllVehicle(request, new Callback<VehicleBackBean>() {
            @Override
            public void success(VehicleBackBean vehicleBackBean, Response response) {

                if (vehicleBackBean.getErrorCode().equals("0") && vehicleBackBean.getData() != null) {
                    backBean = vehicleBackBean;
                    adapter.setVehicleData(vehicleBackBean);
                    for (int i = 0; i < vehicleBackBean.getData().size(); i++) {
                        if (vehicleBackBean.getData().get(i).getIsSelect() == 1) {
                            Picasso.with(BaseApplication.getContext()).load(backBean.getData().get(i).getSnPic().isEmpty() ? null : backBean.getData().get(i).getSnPic()).into(vehcile_img);
                        }
                    }

                }
            }

            @Override
            public void failure(RetrofitError error) {
                ToastUtils.show("网络加载异常", 3);
            }
        });
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        back = bindView(R.id.img_back);
        back.setOnClickListener(this);
        back.setVisibility(View.VISIBLE);
        head = bindView(R.id.head_title);
        head.setVisibility(View.VISIBLE);
        vehcile_img = bindView(R.id.vehicle_imgs);
        bindVehicle_list = bindView(R.id.mybind_list);
        addVehicle = bindView(R.id.add_vehicle);
        swipeRefreshLayout = bindView(R.id.swiplayout);

    }

    @Override
    protected int getId() {
        return R.layout.activity_vehicle;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.add_vehicle:
                RegisterThirdActivity.RegisterThirdIntent(this, 1);
                break;
        }
    }

    @Override
    public void setChange() {
        getDataFromSer();
    }

    @Override
    public void onRefresh() {
        getDataFromSer();
        swipeRefreshLayout.setRefreshing(false);
    }
}
